package com.itheima.student.dao.Impl;

import com.itheima.domain.Student;
import com.itheima.student.dao.IStudentDao;

import java.io.*;
import java.util.ArrayList;

public class StudentDaoImpl implements IStudentDao {

    private ArrayList<Student> students = new ArrayList<>();//该集合的初始为文件中的集合对象

    /*
    增加
    */
    @Override
    public void addStudent(Student student) {
        students.add(student);

    }


    /*
    删除
    */
    //删除指定学号
    @Override
    public void deleteSidStudent(String sid) {
        students.remove(getIndex(sid));
    }

    //删除全部学生
    public void deleteAllStudent() {
        students.clear();//清空集合
    }


    /*
    改
    */
    @Override
    public void updateStudent(String sid, Student student) {
        students.set(getIndex(sid), student);
    }


    /*
    查询
    */
    @Override
    public ArrayList<Student> findAllStudent() {
        return students;
    }

    //按学号查学生
    @Override
    public Student findSidStudent(String sid) {
        return students.get(getIndex(sid));
    }

    //按姓名查学生
    @Override
    public ArrayList<Student> findNameStudent(String name) {
        ArrayList<Student> newStudents = new ArrayList<>();
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getName().equals(name)) {
                newStudents.add(students.get(i));
            }
        }
        return newStudents;
    }

    //按年龄查学生
    @Override
    public ArrayList<Student> findAgeStudent(int age) {
        ArrayList<Student> newStudents = new ArrayList<>();
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getAge() == age) {
                newStudents.add(students.get(i));
            }
        }
        return newStudents;
    }

    //按手机号查学生
    @Override
    public ArrayList<Student> findPhoneStudent(String phone) {
        ArrayList<Student> newStudents = new ArrayList<>();
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getPhone().equals(phone)) {
                newStudents.add(students.get(i));
            }
        }
        return newStudents;
    }

    //按组号查学生
    @Override
    public ArrayList<Student> findGroupStudent(int group) {
        ArrayList<Student> newStudents = new ArrayList<>();
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getGroup() == group) {
                newStudents.add(students.get(i));
            }
        }
        return newStudents;
    }


    //根据学号获取索引
    @Override
    public int getIndex(String sid) {
        for (int i = 0; i < students.size(); i++) {
            if (sid.equals(students.get(i).getSid())) {
                return i;
            }
        }
        return -1;
    }


    //将学生永久存储文件
    @Override
    public void writeStudent() throws IOException {
        //1.创建序列化流对象
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("system\\message\\StudentMessage.txt"));
        oos.writeObject(students);
        oos.close();
        //Serializable是一个标记接口，实现该接口，不需要重写任何方法
    }

    //将文件的学生对象读取到集合中
    @Override
    public void readStudent() throws Exception {
        // 反序列化
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream("system\\message\\StudentMessage.txt"));
            Object o = ois.readObject();
            students = (ArrayList<Student>) o;
        } catch (Exception e) {
        } finally {
            if (ois != null) {
                ois.close();
            }
        }
    }
}
